int kmp(char MainStr[], char SubStr[])
//int kmp(string MainStr, string SubStr)
{
	//获取子串长度
	int tmp = 0;
	while (SubStr[tmp] != '\0')
	{
		tmp++;
	}

	//temp数组找值
	int* temp = new int[tmp];
	fill(temp, temp + tmp, 0);
	temp[0] = -1;
	for (int i = 2; i < tmp; ++i)
	{
		for (int j = i - 1, n = 1; j >= 1; --j, ++n)
		{
			int count = 0;
			for (int k = 0; k < j; ++k)
			{
				if (SubStr[k] == SubStr[k + n])
				{
					count++;
				}
			}
			if (count == j)
			{
				temp[i] = count;
				break;
			}
		}
	}

	//KMP核心算法
	int i = 0, j = 0;
	while (MainStr[i] != '\0' && SubStr[j] != '\0')
	{
		if (MainStr[i] == SubStr[j])
		{
			i++;
			j++;
		}
		else
		{
			j = temp[j];//j下标回溯至temp[j]储存的位置
		}

		//如果j下标回溯到了-1值，i、j下标同时加一
		if (j == -1)
		{
			i++;
			j++;
		}
	}

	delete[] temp;

	//比对完成，成功则返回开始位置，否则返回0值
	if (SubStr[j] == '\0')
	{
		return i - j + 1;
	}
	else
	{
		return 0;
	}
}